/*
*	Copyright (C) 2014  Julio Montes, Mario Carrillo
*
*	Julio Montes e-mail: imc.coder@gmail.com
*
*	Mario Carrillo e-mail: mario_90ago@live.com.mx
*
*	BSD 3-Clause License
*/

#include "sdk/include/util.h"

#include "sdk/include/logdlg.h"

#include <wx/tokenzr.h>
#include <wx/file.h>

#include <map>

namespace sdk
{
    bool stringToBool( const wxString &str )
    {
        long number = 0;
        bool result = str.ToLong( &number );
        return ( result && number != 0 ? true : false );
    }

    wxColour stringToColor( const wxString &colorString )
    {
        wxColour l_color( colorString );
        if( l_color.IsOk() )
            return l_color;

        return wxColour( 0, 0, 0 );
    }

    void errorDialog( const wxString &title, const wxString &error, const wxString &details, const wxString &help )
    {
        sdk::LogDialog dlg( title, error, details, help );
        dlg.ShowModal();
    }

    void debugMessage( const char *file, const char *func, int line, const wxString &msg )
    {
        wxString message = wxString::FromUTF8( file ).AfterLast( wxT( '/' ) ) + wxT( "(" ) +
                        wxString::FromUTF8( func ) + wxT( "):" ) +
                        wxString::Format( wxT( "%d message: " ), line ) + msg;
        wxMessageBox( message, wxT( "[DEBUG]" ), wxCENTRE | wxOK | wxICON_EXCLAMATION );
        fprintf( stderr, "[DEBUG] %ls\n", message.wc_str() );
    }

    void debugMessageConsole( const char *file, const char *func, int line, const wxString &msg )
    {
        wxString message = wxString::FromUTF8( file ).AfterLast( wxT( '/' ) ) + wxT( "(" ) +
                        wxString::FromUTF8( func ) + wxT( "):" ) +
                        wxString::Format( wxT( "%d message: " ), line ) + msg;
        fprintf( stderr, "[DEBUG] %ls\n", message.wc_str() );
    }

    bool mkDir( const wxString &dir )
    {
        if( dir != wxEmptyString && !wxDirExists( dir ) )
        {
            wxStringTokenizer st( dir, wxT( "/" ), wxTOKEN_STRTOK );
            wxString currentDir;
            if( dir.StartsWith( _T( "/" ) ) )
                currentDir = _T( '/' );

            while( st.HasMoreTokens() )
            {
                const wxString tok = st.GetNextToken();

                if( !wxDirExists( currentDir + tok ) )
                {
                    if( !wxMkdir( currentDir + tok + wxT( "/" ) ) )
                    {
                        sdk::errorDialog( wxT( "Error" ), wxT( "Cannont create directory: " ) + dir, wxT( "Cannont create directory" ),
                                wxT( "1. Check if you have write permissions\n\n2. Verify that the directory is correct" ) );

                        return false;
                    }
                }
                currentDir += tok + wxT( "/" );
            }
        }

        return true;
    }

    bool mkFile( const wxString &file )
    {
        if( sdk::mkDir( file.BeforeLast( wxT( '/' ) ) ) )
        {
            wxFile f;
            return f.Create( file, true );
        }

        return false;
    }

    size_t getId( const wxString &key )
    {
        static std::map<const wxString,size_t> keys;
        static size_t countKeys = 1;
        const size_t value( keys[key] );

        if( value != 0 )
            return value;
        else
            keys[key]=countKeys;

        return countKeys++;
    }
}

